#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <unistd.h>
#include <cstring>

int main(int args, char* argv[])
{
    if(args != 3)
    {
        std::cout << "client parameter error!!!" << std::endl;
        exit(-1);
    }

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0) exit(-2);

    while(true)
    {
        // sendto
        struct sockaddr_in server;
        memset(&server,0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(argv[1]);
        server.sin_port = htons(std::stoi(argv[2]));

        std::string information;
        std::getline(std::cin, information);
        ssize_t n = ::sendto(sockfd, information.c_str(), sizeof(n), 0, (struct sockaddr*)&server, sizeof(server));
        if(n < 0)
        {
            perror("sendto");
            exit(-3);
        }
        // recvfrom
        char inbuffer[1024];
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        ssize_t m = ::recvfrom(sockfd, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&temp, &len);
        inbuffer[m] = 0;
        if(m < 0)
        {
            perror("recvfrom");
            exit(-4);
        }
        std::cout << "[lazy_server say]#" << inbuffer;
    }
    return 0;
}